/**
 * Chesney's Fireplace System Includes New Supplier, New Fireplace Item, Supplier and Fireplace Update, 
 * Supplier and Fireplace Reports, Login, main menu and Tutorial.
 * 
 * It also Includes Database connection, Database Extension and the two classes which are MySupplier and My Fireplace.
 *  
 * Copyright (C) Bournemouth University 
 * 
 * @author Ruth Shepperd, Ari Lopez
 * 
 * @version 1.0
 */
package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.BevelBorder;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.DefaultComboBoxModel;

import Database.DatabaseExtended;
import Database.MyFireplace;
import Database.MySupplier;
import Database.Database;

public class NewItemScreen extends JFrame {
	//Attributes
	private JPanel contentPane;
	private String id;
	private JTextField unitPriceTextField;
	private JTextField styleTextField;
	private JTextField finishTextField;
	private JTextField iNameTextField;
	private JTextArea descriptionTextArea;
	private JButton btnSubmitAndSave;
	private JButton btnClear;
	private JButton btnMainMenu;
	JComboBox<MySupplier> supplierNameComboBox;
	ComboBoxModel<MySupplier> cModel;
	private MySupplier s;
	private MySupplier[] supplier;
	private String SupplierID;
	private MyFireplace fireplace;
	DefaultComboBoxModel<MySupplier> model;
	private JTextField stockTextField;
	private JFrame thisFrame = this;
	
	/**
	 * Constructor method to set all of the fireplace details
	 * 
	 * @param fireplace (MyFireplace)
	 */
	public NewItemScreen(MyFireplace fireplace) {

		this();
		this.fireplace = fireplace;
		
		id = fireplace.getFireplaceID();
		iNameTextField.setText(fireplace.getFireplaceName());
		descriptionTextArea.setText(fireplace.getDescription());
		finishTextField.setText(fireplace.getFinish());
		styleTextField.setText(fireplace.getStyle());
		unitPriceTextField.setText(String.valueOf(fireplace.getItemPrice()));
		stockTextField.setText(String.valueOf(fireplace.getItemPrice()));

	}

	/**
	 * Create the frame.
	 */
	public NewItemScreen() {
		setVisible(true);
		setTitle("New Fireplace Item");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 506);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setResizable(false);
		
		//Supplier name Combobox
		supplierNameComboBox = new JComboBox<MySupplier>();

		MySupplier suppliers[] = DatabaseExtended.getSuppliers();
		model = new DefaultComboBoxModel<MySupplier>();

		//System.out.println("Supplier Size: " + suppliers.length);
		for (MySupplier s : suppliers) {
			model.addElement(s);
			//System.out.println(s.toString());
		}
		supplierNameComboBox.setModel(model);

		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Enter Item Details",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		//Save and Submit Button
		btnSubmitAndSave = new JButton("Submit and Save");
		btnSubmitAndSave.setHorizontalAlignment(SwingConstants.LEFT);
		btnSubmitAndSave.addActionListener(new ActionListener() {
			/**
			 * Get the data of all details
			 */
			public void actionPerformed(ActionEvent arg0) {
				//Set up new array that will contain the input data of the screen
				String[] data = new String[8];
				//if id is not null set up to null
				try {
					data[0] = fireplace.getFireplaceID();
				} catch (Exception e) {
					data[0] = null;
				}
				//get the data from the screen fields and put in the array.
				data[1] = iNameTextField.getText();
				data[2] = descriptionTextArea.getText();
				data[3] = finishTextField.getText();
				data[4] = styleTextField.getText();
				data[5] = unitPriceTextField.getText();
				data[6] = stockTextField.getText();
				MySupplier supplier = (MySupplier) supplierNameComboBox
						.getSelectedItem();
				data[7] = supplier.getSupplierID();
				//If price field empty enter 0, otherwise database exception
				if (data[5].equals("")) {
					data[5] = "0";
				}
				//if stock field empty enter 0 otherwise database exception
				if (data[6].equals("")) {
					data[6] = "0";
				}
				//if fireplace name empty warning dialogue
				if (data[1].equals("")) {
					JOptionPane.showMessageDialog(null,
							"The fireplace name field cannot be empty");
				//otherwise use the svafireplace method to create the new item
				} else {
					try {
						DatabaseExtended.saveFireplace(new MyFireplace(data));
						//and dispose the frame
						thisFrame.dispose();
					//warning dialogue if enter wrong data type in numerical fields
					} catch (Exception e) {
						JOptionPane
								.showMessageDialog(null,
										"Please enter a numerical value in stock and price field");
					}
				}
			}
		});

		//Clear Button
		btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			/**
			 * Clear the text-fields
			 */
			public void actionPerformed(ActionEvent e) {
				iNameTextField.setText("");
				descriptionTextArea.setText("");
				finishTextField.setText("");
				styleTextField.setText("");
				unitPriceTextField.setText("");
				stockTextField.setText("");
			}
		});

		//MainMenu Button
		btnMainMenu = new JButton("Main Menu");
		btnMainMenu.addActionListener(new ActionListener() {
			/**
			 * Dispose the screen
			 */
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addComponent(btnSubmitAndSave)
							.addGap(65)
							.addComponent(btnClear)
							.addPreferredGap(ComponentPlacement.RELATED, 96, Short.MAX_VALUE)
							.addComponent(btnMainMenu))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addComponent(panel, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(20)
							.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 396, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 328, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnSubmitAndSave, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnMainMenu, GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
						.addComponent(btnClear, GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE))
					.addContainerGap())
		);

		JLabel lblItemName = new JLabel("Item Name:");

		JLabel lblDescription = new JLabel("Description:");

		JLabel lblFinish = new JLabel("Finish:");

		JLabel lblStyle = new JLabel("Style:");

		JLabel lblUnitPrice = new JLabel("Unit Price:");

		JLabel lblStockQuantity = new JLabel("Stock Quantity:");

		JLabel lblSupplierName = new JLabel("Supplier Name:");

		unitPriceTextField = new JTextField();
		unitPriceTextField.setColumns(10);

		styleTextField = new JTextField();
		styleTextField.setColumns(10);

		finishTextField = new JTextField();
		finishTextField.setColumns(10);

		descriptionTextArea = new JTextArea();
		descriptionTextArea.setLineWrap(true);
		descriptionTextArea.setWrapStyleWord(true);

		iNameTextField = new JTextField();
		iNameTextField.setColumns(10);

		stockTextField = new JTextField();
		stockTextField.setColumns(10);

		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1
				.setHorizontalGroup(gl_panel_1
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_1
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																lblStockQuantity)
														.addComponent(
																lblSupplierName)
														.addComponent(
																lblUnitPrice)
														.addComponent(lblStyle)
														.addComponent(lblFinish)
														.addComponent(
																lblDescription)
														.addComponent(
																lblItemName))
										.addGap(18)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_1
																		.createParallelGroup(
																				Alignment.TRAILING,
																				false)
																		.addComponent(
																				finishTextField,
																				Alignment.LEADING)
																		.addComponent(
																				styleTextField,
																				Alignment.LEADING))
														.addGroup(
																gl_panel_1
																		.createParallelGroup(
																				Alignment.TRAILING,
																				false)
																		.addComponent(
																				iNameTextField,
																				Alignment.LEADING)
																		.addComponent(
																				descriptionTextArea,
																				Alignment.LEADING,
																				GroupLayout.DEFAULT_SIZE,
																				194,
																				Short.MAX_VALUE))
														.addComponent(
																supplierNameComboBox,
																GroupLayout.PREFERRED_SIZE,
																188,
																GroupLayout.PREFERRED_SIZE)
														.addGroup(
																gl_panel_1
																		.createParallelGroup(
																				Alignment.TRAILING,
																				false)
																		.addComponent(
																				stockTextField,
																				Alignment.LEADING)
																		.addComponent(
																				unitPriceTextField,
																				Alignment.LEADING,
																				GroupLayout.DEFAULT_SIZE,
																				107,
																				Short.MAX_VALUE)))
										.addContainerGap(77, Short.MAX_VALUE)));
		gl_panel_1
				.setVerticalGroup(gl_panel_1
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_1
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblItemName)
														.addComponent(
																iNameTextField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblDescription)
														.addComponent(
																descriptionTextArea,
																GroupLayout.PREFERRED_SIZE,
																54,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(lblFinish)
														.addComponent(
																finishTextField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(lblStyle)
														.addComponent(
																styleTextField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblUnitPrice)
														.addComponent(
																unitPriceTextField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblStockQuantity)
														.addComponent(
																stockTextField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblSupplierName)
														.addComponent(
																supplierNameComboBox,
																GroupLayout.PREFERRED_SIZE,
																21,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		panel_1.setLayout(gl_panel_1);

		JLabel lblNewFireplaceItem = new JLabel("New Fireplace Item");
		lblNewFireplaceItem.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewFireplaceItem.setFont(new Font("Arial", Font.BOLD, 18));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblNewFireplaceItem,
								GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
						.addContainerGap()));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblNewFireplaceItem,
								GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
						.addContainerGap()));
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}
}
